package com.example.alarmcontrolpanel;

import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class DataBaseAdapter
{	
	// TODO: Create public field for each column in your table.
	// SQL Statement to create a new database.
	
	// Variable to hold the database instance
	SQLiteDatabase db;
	
	
	private MyAlarmDatabase myAlarmDB;
	
	public DataBaseAdapter(Context context)
	{
		myAlarmDB = new MyAlarmDatabase(context);
	}
	public DataBaseAdapter open() throws SQLException 
	{
		try
		{
			db = myAlarmDB.getWritableDatabase();
		}
		catch (SQLiteException ex)
		{
			db = myAlarmDB.getReadableDatabase();
			System.out.println(ex.toString());
		}
		return this;
	}
	public void close()
	{
		myAlarmDB.close();
	}
	public int insert(AlramRecord alarm)
	{
		open();
		String str = new String("");
		for(int i=0;i<7;i++)
		{
			str += String.valueOf(alarm.repeatDay[i]);
			
		}
		ContentValues newRow = new ContentValues();
		newRow.put("description", alarm.desc);
		newRow.put("repeatType", alarm.repeatType);
		newRow.put("repeatDay", str);
		newRow.put("millis", Long.toString(alarm.Cal.getTimeInMillis()));
		db.insert("AlarmDataBase", null, newRow);
		close();
		return getLastIdNumber();
	}
	public AlramRecord[] getAll()
	{
		open();
		int alarm_id;
		String desc;
		int[] repeatDay;
		int repeatType;
		Calendar Cal;
		
		
		AlramRecord[] alarms;
		String[] columList = {"alarm_id","description", "repeatType" ,"repeatDay" ,"millis"};
		//query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
		Cursor rowList = db.query("AlarmDataBase", columList, null, null, null, null, null);
		
		rowList.moveToFirst();
		
		
		if(rowList.getColumnCount()>0)
		{
			String days;
			int[] b = new int[7];
			int i=0;
			alarms = new AlramRecord[rowList.getColumnCount()];
			rowList.moveToFirst();
			do
			{
				alarm_id    =    rowList.getInt(0);
				desc        =    rowList.getString(1);
				repeatType  =    rowList.getInt(2);
				Cal         =    Calendar.getInstance();
				Cal.setTimeInMillis((Long.parseLong(rowList.getString(4))));
				days = rowList.getString(3);
				for(int i1=0;i1<7;i1++)
				{
					b[i1]=Integer.parseInt(Character.toString(days.charAt(i1)));
				}
				repeatDay=b;
				alarms[i]=new AlramRecord(alarm_id, desc, repeatDay, repeatType, Cal);
				
				i++;
			}while(rowList.moveToNext());
			System.out.println("Hello"+alarms);
			close();
			return alarms;
		}
		else
		{
			close();
			return null;
		}
	}
	public AlramRecord getAlarm(int id)
	{
		open();
		int alarm_id;
		String desc;
		int[] repeatDay;
		int repeatType;
		Calendar Cal;
		
		AlramRecord alarm;
		String[] columList = {"alarm_id","description", "repeatType" ,"repeatDay" ,"millis"};
		//query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
		Cursor rowList = db.query("AlarmDataBase", columList, "alarm_id="+id, null, null, null, null);
		rowList.moveToFirst();
		
		if(rowList.getColumnCount()>0)
		{
			alarm_id=rowList.getInt(0);
			desc=rowList.getString(1);
			repeatType=rowList.getInt(2);
			Cal= Calendar.getInstance();
			Cal.setTimeInMillis((Long.parseLong(rowList.getString(4))));
			String days = rowList.getString(3);
			int[] b=new int[7];
			for(int i1=0;i1<7;i1++)
			{
				b[i1]=Integer.parseInt(Character.toString(days.charAt(i1)));
			}
			repeatDay=b;
			alarm=new AlramRecord(alarm_id, desc, repeatDay, repeatType, Cal);
			close();
			return alarm;
		}
		else
		{
			close();
			return null;
		}
	}
	public int getLastIdNumber()
	{
		open();
		String[] columList = {"alarm_id","description", "repeatType" ,"repeatDay" ,"millis"};
		//query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
		Cursor rowList = db.query("AlarmDataBase", columList, null, null, null, null, null);
		rowList.moveToLast();
		close();
		return rowList.getInt(0);
	}
	public int getTotalNumberOfRows()
	{
		open();
		String[] columList = {"alarm_id","description", "repeatType" ,"repeatDay" ,"millis"};
		//query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
		Cursor rowList = db.query("AlarmDataBase", columList, null, null, null, null, null);
		close();
		return rowList.getColumnCount();
	}
}
